package com.hw.springcloud.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hw.springcloud.dao.StorageDao;
import com.hw.springcloud.entity.StorageEntity;
import com.hw.springcloud.service.StorageService;
import org.springframework.stereotype.Service;

/**
 * @Classname: StorageServiceImpl
 * @Description: 描述
 * @Date: 2021/12/22
 * @Author: HuWei
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageDao, StorageEntity> implements StorageService {

    @Override
    public Boolean UpdateStorage(Long productId, Integer userNum) {
        LambdaQueryWrapper<StorageEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StorageEntity::getProductId,productId);
        StorageEntity one = this.getOne(queryWrapper);
        // 计算已用库存
        Integer user = one.getUsed() + userNum;
        one.setUsed(user);
        // 计算剩余库存
        one.setResidue(one.getTotal() - user);
        // 更新库存
        return this.updateById(one);
    }
}
